home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0158_Click and drag in a TListbox?.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  11.2 KB  |  269 lines

  1.  
  2. {coolbox.pas}
  3. {A note from the author:
  4.    I needed to do some spiffy things with the listboxes so I wrote this.  If it already exists, then great, but I couldn't find it.  With this small program, you can multi-select items from ListBox1 and drag them to ListBox2. No big deal, except that you
  5.    The really cool thing here is that you can select an item in ListBox2 and move it into a another spot within the list by using the arrows or by dragging and dropping.  Again, I couldn't find any code that already did this.  I hope you find this code us
  6.  
  7. Richard Howard 71553,2544
  8. Mei Technology Corporation
  9. 26 August 1995}
  10.  
  11. unit CoolBox;
  12. interface
  13. uses
  14.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  15.   Forms, Dialogs, StdCtrls, Buttons, Spin;
  16. type
  17.   TForm1 = class(TForm)
  18.     ListBox1: TListBox;
  19.     ListBox2: TListBox;
  20.     SpinButton1: TSpinButton; {for moving items in listbox2.}
  21.     procedure ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
  22.       State: TDragState; var Accept: Boolean);
  23.     procedure ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
  24.     procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
  25.       Shift: TShiftState; X, Y: Integer);
  26.     procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  27.       State: TDragState; var Accept: Boolean);
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure MoveUp(Sender: TObject);
  30.     procedure MoveDown(Sender: TObject);
  31.   private
  32.     { Private declarations }
  33.   public
  34.     { Public declarations }
  35.   end;
  36. var
  37.   Form1: TForm1;
  38.   MoveSelectedItem : Integer; {the item in ListBox2 being moved}
  39.   DnListBox1 : Boolean; {indicates which listbox to work with}
  40.   DnListBox2 : Boolean; {indicates which listbox to work with}
  41. implementation
  42. {$R *.DFM}
  43. procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
  44.   State: TDragState; var Accept: Boolean);
  45. begin
  46.   if (Source is TListBox) then Accept := True;
  47.   {because this is such a small program, 'ACCEPT := True' would work.  But
  48.   larger programs need a little more control.}
  49. end;
  50. procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
  51. var
  52.   i : Integer; {serves two purposes: 1) a counting variable for ListBox1,
  53.                and 2) the item that the SELECTED item is being dropped on to
  54.                in ListBox 2}
  55. begin {procedure}
  56.   {instructions for moving items from ListBox1 to ListBox2}
  57.   if DnListBox1 then
  58.   begin {if 1}
  59.     for i := 0 to ListBox1.Items.Count - 1 do {look at ALL items in ListBox1}
  60.     begin {for}
  61.       if ListBox1.Selected[i] then
  62.         ListBox2.Items.Insert(ListBox2.ItemAtPos(Point(X,Y), True), ListBox1.Items[i]);
  63.         ListBox1.Selected[i] := False; {after copying to LB2, UNselect it}
  64.     end; {for}
  65.    DnListBox1 := False;
  66.   end; {if 1}
  67.   {instructions for moving an item WITHIN ListBox2}
  68.   if DnListBox2 then
  69.   begin {if 2}
  70.     {i = the item UNDER the moving, selected item}
  71.     i := ListBox2.ItemAtPos(Point(X, Y), True);
  72.     ListBox2.Items.Move(MoveSelectedItem, i); {puts the moved item into place}
  73.     ListBox2.ItemIndex := i; {select (highlight) the item you moved}
  74.     if i = -1 then ListBox2.ItemIndex := ListBox2.Items.Count-1;
  75.     DnListBox2 := False;
  76.   end; {if 2}
  77. end; {procedure}
  78. procedure TForm1.ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
  79.   Shift: TShiftState; X, Y: Integer);
  80. begin {procedure}
  81.   DnListBox1 := False;{tells the OnDragDrop procedure which instructions to use}
  82.   DnListBox2 := True;{tells the OnDragDrop procedure which instructions to use}
  83.   if Button = mbLeft then
  84.     if ListBox2.ItemAtPos(Point(X, Y), True) >= 0 then
  85.       MoveSelectedItem := ListBox2.ItemIndex;
  86. end; {procedure}
  87. procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  88.   State: TDragState; var Accept: Boolean);
  89. begin
  90.   DnListBox1 := True;
  91. end;
  92. procedure TForm1.FormCreate(Sender: TObject);
  93. begin
  94.   {I just threw these in here to look nice.  They can be pretty handy.}
  95.   SendMessage(ListBox1.Handle, LB_SetHorizontalExtent, 1000, LongInt(0));
  96.   SendMessage(ListBox2.Handle, LB_SetHorizontalExtent, 1000, LongInt(0));
  97. end;
  98. procedure TForm1.MoveUp(Sender: TObject);
  99. var
  100.   i : Integer;
  101. begin {procedure}
  102.   if ListBox2.ItemIndex > 0 then
  103.   begin {if}
  104.     i := ListBox2.ItemIndex;
  105.     ListBox2.Items.Move(i, i-1);
  106.     ListBox2.ItemIndex := i-1;
  107.   end; {if}
  108. end; {procedure}
  109. procedure TForm1.MoveDown(Sender: TObject);
  110. var
  111.   i : Integer;
  112. begin {procedure}
  113.   if (ListBox2.ItemIndex < ListBox2.Items.Count-1) and
  114.      (ListBox2.ItemIndex <> -1) then
  115.   begin {if}
  116.     i := ListBox2.ItemIndex;
  117.     ListBox2.Items.Move(i, i+1);
  118.     ListBox2.ItemIndex := i+1;
  119.   end; {if}
  120. end; {procedure}
  121. end.
  122. {*********************}
  123. {coolproj.dpr}
  124. program Coolproj;
  125. uses
  126.   Forms,
  127.   Coolbox in 'COOLBOX.PAS' {Form1};
  128. {$R *.RES}
  129. begin
  130.   Application.CreateForm(TForm1, Form1);
  131.   Application.Run;
  132. end.
  133. {*********************}
  134. {Coolbox.dfm}
  135. object Form1: TForm1
  136.   Left = 245
  137.   Top = 163
  138.   Width = 349
  139.   Height = 253
  140.   Caption = 'Form1'
  141.   Font.Color = clWindowText
  142.   Font.Height = -13
  143.   Font.Name = 'System'
  144.   Font.Style = []
  145.   PixelsPerInch = 96
  146.   OnCreate = FormCreate
  147.   TextHeight = 16
  148.   object ListBox1: TListBox
  149.     Left = 16
  150.     Top = 24
  151.     Width = 129
  152.     Height = 177
  153.     DragMode = dmAutomatic
  154.     ItemHeight = 16
  155.     Items.Strings = (
  156.       'List 1'
  157.       'List 2'
  158.       'List 3'
  159.       'List 4'
  160.       'List 5'
  161.       'List 6')
  162.     MultiSelect = True
  163.     TabOrder = 0
  164.     OnDragOver = ListBox1DragOver
  165.   end
  166.   object ListBox2: TListBox
  167.     Left = 168
  168.     Top = 24
  169.     Width = 129
  170.     Height = 177
  171.     DragMode = dmAutomatic
  172.     ItemHeight = 16
  173.     Items.Strings = (
  174.       'Test 1'
  175.       'Test 2'
  176.       'Test 3'
  177.       'Test 4')
  178.     TabOrder = 1
  179.     OnDragDrop = ListBox2DragDrop
  180.     OnDragOver = ListBox2DragOver
  181.     OnMouseDown = ListBox2MouseDown
  182.   end
  183.   object SpinButton1: TSpinButton
  184.     Left = 308
  185.     Top = 76
  186.     Width = 20
  187.     Height = 53
  188.     DownGlyph.Data = {
  189.       7E040000424D7E04000000000000360400002800000009000000060000000100
  190.       0800000000004800000000000000000000000000000000000000000000000000
  191.       80000080000000808000800000008000800080800000C0C0C00061898D00A5BF
  192.       C200000000000000000000000000000000000000000000000000000000000000
  193.       0000000000000000000000000000000000000000000000000000000000000000
  194.       0000000000000000000000000000000000000000000000000000000000000000
  195.       0000000000000000000000000000000000000000000000000000000000000000
  196.       0000000000000000000000000000000000000000000000000000000000000000
  197.       0000000000000000000000000000000000000000000000000000000000000000
  198.       0000000000000000000000000000000000000000000000000000000000000000
  199.       0000000000000000000000000000000000000000000000000000000000000000
  200.       0000000000000000000000000000000000000000000000000000000000000000
  201.       0000000000000000000000000000000000000000000000000000000000000000
  202.       0000000000000000000000000000000000000000000000000000000000000000
  203.       0000000000000000000000000000000000000000000000000000000000000000
  204.       0000000000000000000000000000000000000000000000000000000000000000
  205.       0000000000000000000000000000000000000000000000000000000000000000
  206.       0000000000000000000000000000000000000000000000000000000000000000
  207.       0000000000000000000000000000000000000000000000000000000000000000
  208.       0000000000000000000000000000000000000000000000000000000000000000
  209.       0000000000000000000000000000000000000000000000000000000000000000
  210.       0000000000000000000000000000000000000000000000000000000000000000
  211.       0000000000000000000000000000000000000000000000000000000000000000
  212.       0000000000000000000000000000000000000000000000000000000000000000
  213.       0000000000000000000000000000000000000000000000000000000000000000
  214.       0000000000000000000000000000000000000000000000000000000000000000
  215.       0000000000000000000000000000000000000000000000000000000000000000
  216.       0000000000000000000000000000000000000000000000000000000000000000
  217.       0000000000000000000000000000000000000000000000000000000000000000
  218.       0000000000000000000000000000000000000000000000000000000000000000
  219.       0000000000000000000000000000000000000000000000000000000000000000
  220.       0000000000000000000000000000000000000000000000000000000000000000
  221.       000000000000000000000000000000000000D2E0E100A4A0A000808080000000
  222.       FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00030303030303
  223.       0303030000000303030300030303030000000303030000000303030000000303
  224.       0000000000030300000003000000000000000300000003030303030303030300
  225.       0000}
  226.     TabOrder = 2
  227.     UpGlyph.Data = {
  228.       7E040000424D7E04000000000000360400002800000009000000060000000100
  229.       0800000000004800000000000000000000000000000000000000000000000000
  230.       80000080000000808000800000008000800080800000C0C0C00061898D00A5BF
  231.       C200000000000000000000000000000000000000000000000000000000000000
  232.       0000000000000000000000000000000000000000000000000000000000000000
  233.       0000000000000000000000000000000000000000000000000000000000000000
  234.       0000000000000000000000000000000000000000000000000000000000000000
  235.       0000000000000000000000000000000000000000000000000000000000000000
  236.       0000000000000000000000000000000000000000000000000000000000000000
  237.       0000000000000000000000000000000000000000000000000000000000000000
  238.       0000000000000000000000000000000000000000000000000000000000000000
  239.       0000000000000000000000000000000000000000000000000000000000000000
  240.       0000000000000000000000000000000000000000000000000000000000000000
  241.       0000000000000000000000000000000000000000000000000000000000000000
  242.       0000000000000000000000000000000000000000000000000000000000000000
  243.       0000000000000000000000000000000000000000000000000000000000000000
  244.       0000000000000000000000000000000000000000000000000000000000000000
  245.       0000000000000000000000000000000000000000000000000000000000000000
  246.       0000000000000000000000000000000000000000000000000000000000000000
  247.       0000000000000000000000000000000000000000000000000000000000000000
  248.       0000000000000000000000000000000000000000000000000000000000000000
  249.       0000000000000000000000000000000000000000000000000000000000000000
  250.       0000000000000000000000000000000000000000000000000000000000000000
  251.       0000000000000000000000000000000000000000000000000000000000000000
  252.       0000000000000000000000000000000000000000000000000000000000000000
  253.       0000000000000000000000000000000000000000000000000000000000000000
  254.       0000000000000000000000000000000000000000000000000000000000000000
  255.       0000000000000000000000000000000000000000000000000000000000000000
  256.       0000000000000000000000000000000000000000000000000000000000000000
  257.       0000000000000000000000000000000000000000000000000000000000000000
  258.       0000000000000000000000000000000000000000000000000000000000000000
  259.       0000000000000000000000000000000000000000000000000000000000000000
  260.       000000000000000000000000000000000000D2E0E100A4A0A000808080000000
  261.       FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00030303030303
  262.       0303030000000300000000000000030000000303000000000003030000000303
  263.       0300000003030300000003030303000303030300000003030303030303030300
  264.       0000}
  265.     OnDownClick = MoveDown
  266.     OnUpClick = MoveUp
  267.   end
  268. end
  269.